<template>
  <van-pull-refresh
    v-model="isLoading"
    @refresh="onLoad"
  >
    <van-list
      :finished="finished"
      @load="onLoad"
    >
      <van-empty
        v-if="market.length === 0 && finished"
        :description="$t('empty.default')"
      />
      <template v-else>
        <van-cell
          v-for="item in market"
          :key="item.id"
          @click="goDetail(item)"
        >
          <div class="asset-item">
            <div class="center">
              <div class="name">
                <div class="market_name">
                  {{ item.stock }}
                  <span>/{{ item.money}}</span>
                </div>
                <van-tag
                  v-if="findRobot(item.id) && findRobot(item.id).recycle_status == 1"
                  round
                  type="primary"
                >
                  {{ $t('cycle_strategy') }}
                </van-tag>
                <van-tag
                  v-if="findRobot(item.id) && findRobot(item.id).recycle_status == 0"
                  round
                  type="primary"
                >
                  {{ $t('single_strategy') }}
                </van-tag>
              </div>
              <div
                v-if="findRobot(item.id)"
                class="info"
              >
                <p>
                  {{ $t('position_amount') }}：{{ getDeal(item.values_str,'deal_amount') | numberFormat(5)}} {{ item.money }}
                </p>
                <p>
                  {{ $t('average_price') }}：{{ (getDeal(item.values_str,'deal_money') /getDeal(item.values_str,'deal_amount')) |numberFormat(5)}} {{ item.money }}
                </p>
                <p>
                  {{ $t('expected_return') }}：{{ Number(findRobot(item.id).revenue) | numberFormat(5) }} {{ item.money }}
                </p>
                <p >
                  {{ $t('revue_rate') }}：
                  <span v-if="Number(findRobot(item.id).rate) >=0" style="color:green">{{ Number(findRobot(item.id).rate* 100) | numberFormat(5) }}%</span>
                  <span v-else style="color:red">{{ Number(findRobot(item.id).rate) | numberFormat(5) }}%</span>
                </p>
                <p
                  class="van-ellipsis"
                >
                  {{ $t('number_positions') }}：{{ getDeal(findRobot(item.id).values_str) }} {{ item.stock }}
                </p>
              </div>
            </div>
            <div class="right">
              <template v-if="findRobot(item.id)">
                <van-icon name="arrow" />
                <span class="status" v-if="findRobot(item.id)" :class="findRobot(item.id).status === 0?'orange':'young'">
                  {{findRobot(item.id).status === 0? $t('status.disabled') :$t('status.enabled')}}
                </span>
              </template>
              <van-button
                v-else
                size="small"
                icon="plus"
                @click="addRobot(item)"
              >
                <span style="display: inline-block; vertical-align: middle">{{ $t('add_robot') }}</span>
              </van-button>
            </div>
          </div>
        </van-cell>
      </template>
    </van-list>
  </van-pull-refresh>
</template>

<script>
import { mapState, mapActions, mapGetters } from 'vuex'
export default {
  props: {
    platform: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      finished: false,
      isLoading: false
    }
  },
  filters: {
    formatFront(text) {
      let isFormat = text.indexOf("/")
      if(isFormat !== -1){
        return text.substring(0, text.indexOf("/"));
      }else{
        return text
      }
    },
    formatAfter(text) {
      let isFormat = text.lastIndexOf("/")
      if(isFormat !== -1){
        return text.substr(text.lastIndexOf("/"));
      }else{
        return ''
      }
    },
  },
  getDeal (values,id) {
    if (values) {
      const valueJson = JSON.parse(values)
      return Number(valueJson[id]) || '-'
    }
    return '-'
  },
  goDetail (item) {
    this.$nextTick(() => {

      if (item.type == 2) {
        this.$router.push({
          name: 'robot-tract',
          query: { market_id: item.market_id }
        })
      } else {
        this.$router.push({
          name: 'robot',
          query: { market_id: item.market_id }
        })
      }

    })
  },
  computed: {
    ...mapState({
      robotData: ({ robot }) => robot.robotList
    }),
    ...mapGetters({
      markets: 'robot/markets'
    }),
    market () {
      return this.markets(this.platform) || []
    }
  },
  methods: {
    ...mapActions({
      marketList: 'robot/marketList',
      robotList: 'robot/robotList'
    }),
    loadData () {
      this.finished = false
      this.marketList({
        platform: this.platform,
        type: 'spot'
      })
      this.finished = true
      this.isLoading = false
    },
    onLoad () {
      this.loadData()
      this.robotList()
    },
    findRobot (id) {
      return this.robotData.filter(item => item.market_id === id)[0]
    },
    goDetail (item) {
      if (this.findRobot(item.id)) {
        this.$nextTick(() => {
          this.$router.push({
            name: 'robot',
            query: { market_id: item.id }
          })
        })
      }
    },
    addRobot (item) {
      this.$router.push({
        name: 'robot-form',
        query: {
          type: 'create',
          platform: this.platform,
          data: JSON.stringify(item)
        }
      })
    },
    getDeal (values) {
      if (values) {
        const valueJson = JSON.parse(values)
        return Number(valueJson.deal_amount).toFixed(6) || '-'
      }
      return '-'
    },

    getValue (values) {
      if (values) {
        const valueJson = JSON.parse(values)

        return valueJson
      }
      return '-'
    }
  }
}
</script>

<style scoped lang="less">
.van-cell{
  padding: 16px 13px 14px 13px;
  line-height: 1;
  &:after{
    left: 0;
    right: 0;
  }
  .asset-item{
    display: flex;
    align-items: center;
    justify-content: space-between;
    .name{
      display: flex;
      align-items: center;
      .market_name{
        font-size: 20px;
        font-weight: 400;
        color: #000000;
        span{
          font-size: 13px;
          font-weight: 400;
          color: #3871F9;
        }
      }
      .van-tag--round{
        margin-left: 23px;
        height: 18px;
        padding: 0 10px;
        background: linear-gradient(90deg, #366FF9 0%, #6D97FA 100%);
        border-radius: 9px;
      }
    }
    .info{
      margin-top: 10px;
      font-size: 13px;
      font-weight: 400;
      color: #727383;
      line-height: 18px;
      p{
        padding: 2px 0;
      }
    }
    .right{
      padding-bottom: 1px;
      .van-button{
        height: 28px;
        font-size: 12px;
        font-weight: 500;
        color: #3871F9;
        border-radius: 15px;
        box-sizing: border-box;
        border: 1px solid #3871F9;
      }
      .van-icon-arrow {
        font-size: 18px;
        color: #727383;
      }
      .status{
        position: absolute;
        right: 0;
        bottom: 2px;
        line-height: 18px;
        font-size: 12px;
        font-weight: 500;
        &.orange{
          color: #F5A623;
        }
        &.young{
          color: #50E3C2;
        }
      }
    }
  }
}
</style>
